package framework
{
	import flash.events.IEventDispatcher;

	import framework.event.EventToken;

	public function listen(obj:Object, eventType:String, listener:Function, howManyTimes:int = -1, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):EventToken
	{
		var t:EventToken = new EventToken;
		var dispatcher:IEventDispatcher = IEventDispatcher(obj);
		dispatcher.addEventListener(eventType, t.handler, useCapture, priority, useWeakReference);
		t.dispatcher = dispatcher;
		t.eventType = eventType;
		t.listener = listener;
		if (howManyTimes == -1)
		{
			howManyTimes = EventToken.defaultHowManyTimes;
		}
		else if (howManyTimes == 0)
		{
			t.unlimited = true;
		}
		t.times = howManyTimes;
		t.useCapture = useCapture;
		EventToken.eventTokens[dispatcher] = t;
		return t;
	}
}